from datetime import date, datetime, timedelta
import math
from wechatpy import WeChatClient
from wechatpy.client.api import WeChatMessage, WeChatTemplate
import requests
import os
import random
import schedule
from zhdate import ZhDate
# 在github的action中使用这一套
# today = datetime.now() + timedelta(hours=8)
# start_date = os.environ['START_DATE']
# city = os.environ['CITY']
# birthday = os.environ['BIRTHDAY']
#
# app_id = os.environ["APP_ID"]
# app_secret = os.environ["APP_SECRET"]
#
# user_ids = os.environ["USER_ID"].split("\n")
# template_id = os.environ["TEMPLATE_ID"]

today = datetime.now() + timedelta(hours=8)
start_date = "2022-07-19"
city = "广州"
birthday_type = 1     # 农历->0   阳历->1
birthday = (date.today().year, 8, 3)    #自动获取当前年份


def get_citycode():
    #具体到市的天气
    city_shi = "guangzhou"
    url = "https://geoapi.qweather.com/v2/city/lookup?location="+city_shi+"&key=a7774909d4944c4480549c509885c2ed"
    city_information = requests.get(url).json()
    citycode = city_information['location'][0]['id']
    return citycode

def get_weather():
    url = "http://t.weather.sojson.com/api/weather/city/{0}".format(get_citycode())
    res = requests.get(url).json()
    weather = res['data']['forecast'][0]
    return weather['type'], weather['date'], weather['high'],weather['low'],weather['week'],weather['sunrise'],weather['sunset'],weather['notice'],weather['fx'],res['data']['quality']


def get_count():
    a = 123
    delta = today - datetime.strptime(start_date, "%Y-%m-%d")
    return delta.days


def get_birthday():
    global birthday
    if birthday_type==1:
        birthday = str(birthday).replace(', ', '-')
        birthday = birthday[1:-1]
        birthday = datetime.strptime(birthday,"%Y-%m-%d")
        if birthday < datetime.now():
            birthday = birthday.replace(year=birthday.year + 1)
        return (birthday - today).days
    if birthday_type==0:
        next_birthday = ZhDate(*birthday).to_datetime()  # 转化新历
        if next_birthday < datetime.now():  # 判断生日有没有过
            birthday = (date.today().year + 1, birthday[1], birthday[2])
            next_birthday = ZhDate(*birthday).to_datetime()
        return (next_birthday - today).days

def get_words():
    words = requests.get("https://api.shadiao.pro/chp")
    if words.status_code != 200:
        return get_words()
    return words.json()['data']['text']


def get_random_color():
    return "#%06x" % random.randint(0, 0xFFFFFF)

def main():
    client = WeChatClient(app_id, app_secret)

    wm = WeChatMessage(client)
    wea, temperature, max_temperature, min_temperature, week, sunrise, sunset, notice, fengxiang, air_quality = get_weather()
    data = {"area":{"value":city,"color":get_random_color()},
            "calendar":{"value":today.strftime('%Y{a}%m{b}%d{c}').format(a='年',b='月',c='日')+' ' + week, "color":get_random_color()},
            "weather":{"value":wea,"color":get_random_color()},
            "temperature":{"value":"当前气温："+temperature,"color":get_random_color()},
            "wind":{"value":"风向："+fengxiang,"color":get_random_color()},
            "min_temperature": {"value": "最" + min_temperature, "color": get_random_color()},
            "max_temperature":{"value":"最"+max_temperature,"color":get_random_color()},
            "air": {"value": "空气质量：" + air_quality, "color": get_random_color()},
            "sun_go": {"value": "日出时间：" + sunrise, "color": get_random_color()},
            "sun_out": {"value": "日落时间：" + sunset, "color": get_random_color()},
            "love_days":{"value":"今天是我们恋爱的第"+str(get_count())+"天","color":get_random_color()},
            "birthday_left":{"value":"距离宝贝的生日还有"+str(get_birthday())+"天","color":get_random_color()},
            "suggestion":{"value":"今天建议："+notice,"color":get_random_color()},
            "words":{"value":get_words(),"color":get_random_color()},
            }
    count = 0
    for user_id in user_ids:
        res = wm.send_template(user_id, template_id, data)
        count+=1
    print("发送了" + str(count) + "条消息")

# schedule.every().day.at("02:00")    #每天两点执行
schedule.every().minutes.do(main)

while True:
    birthday = (date.today().year, 8, 3)
    schedule.run_pending()


这是制造出来的冲突内容




